﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using WAD.Model;

namespace WAD.DAL.Admin
{
    public class User : DAO
    {

          /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="m"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public static Dictionary<string, object> Login(string username, string pwd)
        {
            //Database db = GetDatabase();
            //DbCommand cmd = db.GetSqlStringCommand("select * from admin where name='" + username + "' and password = '" + pwd + "'");
            //DataSet ds = db.ExecuteDataSet(cmd);
            //try
            //{
            //    if (ds.Tables[0].Rows.Count > 0)
            //    {
            //        return ds.Tables[0].Rows[0]["name"].ToString();
            //    }
            //    else
            //    {
            //        return "";
            //    }
            //}
            //catch { return ""; }

            string where = "name='" + username + "' and password = '" + pwd + "'";
            Dictionary<string, object> model = Get("admin", where, "id,name");
            return model;
        }


        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MUser GetEntityByName(String name)
        {
            String sql = "select * from [user] where name='" + name + "'";
            object obj = BindEntity(sql, new MUser());
            return obj == null ? null : (MUser)obj;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MUser GetEntityById(int id)
        {
            String sql = "select * from [user] where id=" + id;
            object obj = BindEntity(sql, new MUser());
            return obj == null ? null : (MUser)obj;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MUser GetEntityByName(String name,string file)
        {
            if (file == "") return null;
            String sql = "select * from [user] where " + file;
            object obj = BindEntity(sql, new MUser());
            return obj == null ? null : (MUser)obj;
        }
        /// <summary>
        /// 修改用户基本信息
        /// </summary>
        /// <param name="m">哪个有值修改哪个</param>
        /// <returns></returns>
        public static int UpdateInfo(MUser m)
        {
            Database db = GetDatabase();
            DbCommand cmd = db.GetStoredProcCommand("UpdateInfoadmin");

            db.AddInParameter(cmd, "@id", DbType.Int32, m.id);
            db.AddInParameter(cmd, "@name", DbType.String, m.name);
            db.AddInParameter(cmd, "@pwd", DbType.String, m.pwd);
            db.AddInParameter(cmd, "@state", DbType.Int32, m.state == true ? 1 : 0);
            db.AddInParameter(cmd, "@phone", DbType.String, m.phone);
            db.AddInParameter(cmd, "@email", DbType.String, m.email);
            db.AddInParameter(cmd, "@nick_name", DbType.String, m.nick_name);
            db.AddInParameter(cmd, "@qq", DbType.String, m.qq);
            db.AddInParameter(cmd, "@logo", DbType.String, m.logo);
            db.AddInParameter(cmd, "@parent_id", DbType.Int32, m.parent_id);
            db.AddInParameter(cmd, "@agentid", DbType.Int32, m.agentid);
            db.AddOutParameter(cmd, "@code", DbType.Int32, 0);

            DataSet ds = db.ExecuteDataSet(cmd);
            int code = Convert.ToInt32(db.GetParameterValue(cmd, "@code"));
            return code;
        }


    }
}
